﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Microsoft.DirectX.Direct3D;

namespace KGE.Define
{
    [Serializable]
    public class KImage
    {
        public string Name;
        public Guid GUID;
        public string Version;
        public DateTime CreateTime;

        //Point location;
        public Point OPoint;

        public Rectangle DrawRectangle;
        //public Rectangle DrawRectangle
        //{
        //    get
        //    {
        //        Point p = location;

        //        p.X -= OPoint.X;
        //        p.Y -= OPoint.Y;

        //        if (isFlipHorizontal)
        //        {
        //            p.X = -p.X;
        //            p.X -= Image.Size.Width;
        //        }
        //        Rectangle rect = new Rectangle(p, Image.Size);
        //        return rect;
        //    }
        //}
        byte[] data;
        public byte[] Data
        {
            get
            {
                return data;
            }
        }
        public MemoryStream DataStream
        {
            get
            {
                return new MemoryStream(Data);
            }
        }

        [NonSerialized]
        public Bitmap Bitmap;
        [NonSerialized]
        public Texture Texture;
        [NonSerialized]
        public bool IsHorizontalFlip = false;

        public KImage(string fileName)
            : this(Image.FromFile(fileName), new FileIndexer(fileName).Name)
        {
        }
        public KImage(Image image, string name)
            : this(image, name, new Point())
        { }
        public KImage(Image image, string name, Point location)
        {

            GUID = Guid.NewGuid();
            CreateTime = DateTime.Now;

            Name = name;

            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            data = ms.ToArray();
            DrawRectangle.Location = location;
            DrawRectangle.Size = image.Size;
        }
        public void FlipToLeft()
        {
            IsHorizontalFlip = true;
        }
        public void FlipToRight()
        {
            IsHorizontalFlip = false;
        }
    }
}
